package com.neusoft.neuiotms.dcam.anhui.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.common.constant.AHConstants;
import com.neusoft.neuiotms.dcam.constant.DCAMConsts;
import com.neusoft.neuiotms.micro.auth.AuthMicroService;

@Component
public class SearchUtils {

    @Autowired
    AuthMicroService authMicroService;

    /** 查询条件添加组织机构编码查询 */
    public static Searchable addOrgCondition(final Searchable searchable, final UserBean bean) {
        searchable.put("orgs", bean.getOrgs());
        return searchable;
    }

    /** 查询条件添加角色查询 */
    public static Searchable addRoleCondition(final Searchable searchable, final UserBean bean) {
        searchable.put("roles", bean.getRoles().stream().map(s -> s.getRole()).collect(Collectors.toList()));
        return searchable;
    }

    /** 查询条件添加用户编码 */
    public static Searchable addUsernameRoleCondition(final Searchable searchable, final UserBean bean) {
        searchable.put("username", bean.getUsername());
        return searchable;
    }

    /** 查询条件添加科技公司根据角色添加组织机构编码查询 */
    public Searchable addKJRuleOrgCondition(final Searchable searchable, final UserBean bean) {

        List<String> orgs = new ArrayList<>();
        final List<Map<String, Object>> all = this.authMicroService.orgIndex().getData();

        if (PermUtils.isKJAdministrationPrincipal(bean) || PermUtils.isKJAssetAdmin(bean)) {
            orgs = all.stream().map(l -> {
                final String code = l.get("code").toString();
                final String parentCode = l.get("parent") == null ? null : l.get("parent").toString();
                if (AHConstants.BELONG_KJ_CODE.equals(parentCode) || AHConstants.BELONG_KJ_CODE.equals(code)) {
                    return code;
                }

                return null;
            }).filter(s -> null != s).collect(Collectors.toList());
        } else if (PermUtils.isKJDeptAssetAdmin(bean) || PermUtils.isKJDeptPrincipal(bean)) {
            orgs = bean.getOrgs();
        }

        searchable.put("orgs", orgs);
        return searchable;
    }

    /** 查询条件添加省联社根据角色添加组织机构编码查询 */
    public Searchable addSLRuleOrgCondition(final Searchable searchable, final UserBean bean) {

        List<String> orgs = new ArrayList<>();
        final List<Map<String, Object>> all = this.authMicroService.orgIndex().getData();

        if (PermUtils.isSLAssetOwnerAdmin(bean) || PermUtils.isSLAssetOwnerPrincipal(bean)) {
            orgs = all.stream().map(l -> {
                final String code = l.get("code").toString();
                final String parentCode = l.get("parent") == null ? null : l.get("parent").toString();
                if (AHConstants.BELONG_SL_CODE.equals(parentCode) || DCAMConsts.ROOT_STR.equals(code)) {
                    return code;
                }

                return null;
            }).filter(s -> null != s).collect(Collectors.toList());
        } else if (PermUtils.isSLDeptAssetAdmin(bean) || PermUtils.isSLDeptAssetPrincipal(bean)) {
            orgs = bean.getOrgs();
        }

        searchable.put("orgs", orgs);
        return searchable;
    }
}
